package core;

import graphicalInterface.GUIBoard;

import java.util.ArrayList;
import java.util.List;

import pieces.Bishop;
import pieces.King;
import pieces.Knight;
import pieces.Pawn;
import pieces.Piece;
import pieces.Queen;
import pieces.Rook;

/**
 * Class: Board
 * Responsibilities: Keeps track of all chess pieces.
 * Collaborators: GUIBoard and Piece.
 * @author Tobias
 *
 */
public class Board {
	private GUIBoard guiBoard;
	private List<Piece> pieces;

	public List<Piece> getPieces() {
		return pieces;
	}

	public void setPieces(List<Piece> pieces) {
		this.pieces = pieces;
	}




	/**
	 * Setup a board and draw it.
	 */
	public Board() {
		pieces = new ArrayList<Piece>();
		guiBoard = new GUIBoard();
		setupStartBoard();
		guiBoard.drawBoard(this);
	}

	/**
	 * Return a single piece when given a position.
	 * @param position of piece
	 * @return wanted piece
	 */
	public Piece getPiece(Position p) {
		for (Piece i : pieces) {
			if(i.getP().equals(p))
				return i;

		}
		return null;
	}

	/**
	 * Method adding all the pieces to the board.
	 */
	private void setupStartBoard() {

		//White pieces
		pieces.add(new Pawn(Color.WHITE,new Position(1,2))); // White Pawn
		pieces.add(new Pawn(Color.WHITE,new Position(2,2))); // White Pawn
		pieces.add(new Pawn(Color.WHITE,new Position(3,2))); // White Pawn
		pieces.add(new Pawn(Color.WHITE,new Position(4,2))); // White Pawn
		pieces.add(new Pawn(Color.WHITE,new Position(5,2))); // White Pawn
		pieces.add(new Pawn(Color.WHITE,new Position(6,2))); // White Pawn
		pieces.add(new Pawn(Color.WHITE,new Position(7,2))); // White Pawn
		pieces.add(new Pawn(Color.WHITE,new Position(8,2))); // White Pawn

		pieces.add(new King(Color.WHITE,new Position(5,1))); // White king
		pieces.add(new Queen(Color.WHITE,new Position(4,1))); // White queen
		pieces.add(new Knight(Color.WHITE,new Position(2,1))); // White knight
		pieces.add(new Knight(Color.WHITE,new Position(7,1))); // White knight
		pieces.add(new Rook(Color.WHITE,new Position(1,1))); // White rook
		pieces.add(new Rook(Color.WHITE,new Position(8,1))); // White rook
		pieces.add(new Bishop(Color.WHITE,new Position(3,1))); // White bishop
		pieces.add(new Bishop(Color.WHITE,new Position(6,1))); // White bishop


		//Black pieces
		pieces.add(new Pawn(Color.BLACK,new Position(1,7))); // Black Pawn
		pieces.add(new Pawn(Color.BLACK,new Position(2,7))); // Black Pawn
		pieces.add(new Pawn(Color.BLACK,new Position(3,7))); // Black Pawn
		pieces.add(new Pawn(Color.BLACK,new Position(4,7))); // Black Pawn
		pieces.add(new Pawn(Color.BLACK,new Position(5,7))); // Black Pawn
		pieces.add(new Pawn(Color.BLACK,new Position(6,7))); // Black Pawn
		pieces.add(new Pawn(Color.BLACK,new Position(7,7))); // Black Pawn
		pieces.add(new Pawn(Color.BLACK,new Position(8,7))); // Black Pawn

		pieces.add(new King(Color.BLACK,new Position(5,8))); // White king
		pieces.add(new Queen(Color.BLACK,new Position(4,8))); // White queen
		pieces.add(new Knight(Color.BLACK,new Position(2,8))); // White knight #1
		pieces.add(new Knight(Color.BLACK,new Position(7,8))); // White knight #2
		pieces.add(new Rook(Color.BLACK,new Position(1,8))); // White rook #1
		pieces.add(new Rook(Color.BLACK,new Position(8,8))); // White rook #2
		pieces.add(new Bishop(Color.BLACK,new Position(3,8))); // White bishop #1
		pieces.add(new Bishop(Color.BLACK,new Position(6,8))); // White bishop #2

	}

	/**
	 * Check that a player-move is a legal move.
	 * @param Move (given from a Player)
	 * @return True if the move was legal.
	 */
	public boolean isLegalMove(Move m, Color color) {
		if(this.getPiece(m.getFrom()).isWhite() != Color.WHITE.equals(color))
			return false;

		if(m.sameStartAndFinish())
			return false;

		Piece from = getPiece(m.getFrom());

		if(!from.legalMove(m, this)) return false;

		Piece to = getPiece(m.getTo());
		pieces.remove(from);
		from.setP(m.getTo());

		if(to!=null) pieces.remove(to);

		pieces.add(from);
		return true;
	}

	public List<Piece> getPiecesByColor(Color color) {
		List<Piece> foundPieces = new ArrayList<Piece>();
		for (Piece piece : pieces)
			if(piece.isWhite() == Color.WHITE.equals(color))
				foundPieces.add(piece);
		return foundPieces;
	}

	public Piece finished() {
		King white = null;
		King black = null;
		for (Piece p : pieces) {
			if(p instanceof King) {
				if(p.isWhite())
					white = (King) p;
				else
					black = (King) p;
			}
		}
		if(white == null && black != null)
			return black;
		if(white != null && black == null)
			return white;

		return null;
	}

	public GUIBoard getGuiBoard() {
		return guiBoard;
	}

	public void setGuiBoard(GUIBoard guiBoard) {
		this.guiBoard = guiBoard;
	}

}
